package com.zemb.flxwear.UI.CustomView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.zemb.flxwear.R;

public class EcgWindow extends View {
    private Path mPath;
    private float mWidth;
    private float mHeight;
    private Paint mPaint;
    private int mLineColor = Color.GREEN;
    private int mBackgroundColor = Color.WHITE;
    private int speed = 0;
    private int index = 0;

    public EcgWindow(Context context) {
        super(context);
        initData();
    }

    public EcgWindow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData();
        initAttrs(context, attrs);
    }

    public EcgWindow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
        initAttrs(context, attrs);
    }

    private void initData() {
        mPath = new Path();
        mPaint = new Paint();
    }


    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EcgWindow);
        mLineColor = typedArray.getColor(R.styleable.EcgWindow_line_color, context.getResources().getColor(R.color.green));
        mBackgroundColor = typedArray.getColor(R.styleable.EcgWindow_background_color, context.getResources().getColor(R.color.white));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPath(canvas);
        scrollBy(speed, 0);
        //非UI线程下刷新布局，因为UI线程不是线程安全的
        postInvalidateDelayed(10);
        index += speed;
        if (index >= mWidth) {
            scrollBy(-index, 0);
            index = 0;
        }

    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

    }


    private void drawPath(Canvas canvas) {
        // 重置path
        mPath.reset();

        //用path模拟一个心电图样式
        mPath.moveTo(0, mHeight / 2);
        int tmp = 0;
        //第一个周期
        mPath.lineTo(0 + mWidth / 4, mHeight / 2);
        mPath.lineTo(0 + mWidth / 3, mHeight / 4);
        mPath.lineTo(0 + mWidth / 2, mHeight / 2 + mHeight / 18);
        mPath.lineTo(mWidth / 2 + mWidth / 8, mHeight / 2);
        //第二个周期
        mPath.lineTo(mWidth + mWidth / 4, mHeight / 2);
        mPath.lineTo(mWidth + mWidth / 3, mHeight / 4);
        mPath.lineTo(mWidth + mWidth / 2, mHeight / 2 + mHeight / 18);
        mPath.lineTo(mWidth + mWidth / 2 + mWidth / 8, mHeight / 2);
        mPath.lineTo(2 * mWidth, mHeight / 2);

        //设置画笔style
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mLineColor);
        mPaint.setStrokeWidth(5);
        canvas.drawPath(mPath, mPaint);

    }

    //开始动画
    public void startDraw() {
        speed = 2;

    }

    //停止动画
    public void stopDraw() {
        speed = 0;
    }


}


